package com.furong.config;

import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.Bean;

import javax.sql.DataSource;

public class MyBatisConfig {
    @Bean
//    创建SqlSessionFactoryBean
    public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource){
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(dataSource); // 设置数据源
        factoryBean.setTypeAliasesPackage("com.furong.domain"); // 设置实体类 别名 扫描包，自动为该包下的类注册别名
        return factoryBean;
    }

    @Bean
//    创建MapperScannerConfigurer，用于自动扫描mapper接口
    public MapperScannerConfigurer mapperScannerConfigurer(){
        MapperScannerConfigurer msc = new MapperScannerConfigurer();
        msc.setBasePackage("com.furong.dao"); // 设置mapper接口扫描的基础包路径
        return msc;
    }
}
